﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;


public partial class ServicesSheet_PlaneTicketListFeeByDep : System.Web.UI.Page
{
    Srvtools.WebDataSet Plant;
    double sum;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //DSelYearMonth1.SelectYear = DateTime.Now.Year.ToString();
            //DSelYearMonth1.SelectMonth = DateTime.Now.Month.ToString();
            Bind();


            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "";
            master.PageName = "按部门统计机票费用";
            master.PicUrl = "";
            master.IntiBar();
        }
    }
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServicesSheet_PlaneTicketListFeeByDep));
        this.Plant = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.Plant)).BeginInit();
        // 
        // WMaster
        // 
        this.Plant.Active = true;
        this.Plant.AlwaysClose = false;
        this.Plant.PacketRecords = 0;
        this.Plant.Position = -1;
        this.Plant.RemoteName = "Common.InfoInti";
        this.Plant.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.Plant)).EndInit();
    }

    private void Bind()
    {
        string year = DSelYearMonth1.SelectYear;
        string month = DSelYearMonth1.SelectMonth;

        StringBuilder sbr = new StringBuilder();
        StringBuilder sbrgb = new StringBuilder();
        StringBuilder sbrwh = new StringBuilder();

        sbr.Append("select Department.DepartmentName,Department.DepartmentId" +
                    " into #table1" +
                    " from Department where Department.Del=0");

        sbr.Append(" select SUM(TicketPrice) AS Price,Employees.DepartmentId");

        if (year != null && year.ToString() != "" && year.ToString() != "0")
        {
            if (month != null && month.ToString() != "" && month.ToString() != "0")
            {
                //sbr.Append(",Year(Apdate) AS years,Month(Apdate) AS months");

                sbrwh.Append(" And Year(Apdate)='" + year + "' And Month(Apdate)='" + month + "' ");
            }
            else
            {
                sbrwh.Append(" And Year(Apdate)='" + year + "'");
            }
        }
        else
        {
            if (month != null && month.ToString() != "" && month.ToString() != "0")
            {
                //sbr.Append(",Year(Apdate) AS years,Month(Apdate) AS months");

                sbrwh.Append(" And Month(Apdate)='" + month + "' ");
            }

        }


        sbr.Append(" into #table2 from Department");
        sbr.Append(" left join Employees on (Employees.DepartmentId=Department.DepartmentId)");
        sbr.Append(" left join Services_PlaneTicket on (Employees.EmployeeId=Services_PlaneTicket.EmpId)");


        sbr.Append(" where Services_PlaneTicket.FlowFlag='Z'" + sbrwh);
        sbr.Append(" group by Employees.DepartmentId");

        sbr.Append(" select #table1.DepartmentId,#table1.DepartmentName,#table2.Price");
        sbr.Append(" from #table1");
        sbr.Append(" left join #table2 on (#table1.DepartmentId=#table2.DepartmentId)");

        sbr.Append(" if   object_id('tempdb..#table1')   is   not   null   drop   table   #table1");
        sbr.Append(" if   object_id('tempdb..#table2')   is   not   null   drop   table   #table2");

        InitializeComponent();

        //Srvtools.WebDataSet Plant = new Srvtools.WebDataSet();
        wgvMaster.DataSource = Plant.Execute(sbr.ToString()).Tables[0];
        wgvMaster.DataBind();

        //((TextBox)wgvMaster.FindControl("Money")).Text = sum.ToString();
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind();
    }
    protected void wgvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (drv["Price"] != null && drv["Price"].ToString() != "" && drv["Price"].ToString() != "0")
            {
                sum += Convert.ToDouble(drv["Price"]);
            }
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[2].Text = "总计：" + string.Format("{0:C}", sum);
            // for the Footer, display the running totals



        }
    }
}
